﻿// Write a program that reads two integer numbers N and K and an array of N elements from the console. 
// Find in the array those K elements that have maximal sum.

using System;

class FindElementsWithMaximalSum
{
    static void Main()
    {
        // read N
        uint arraySize;
        do
        {
            Console.Write("Enter array size (N): ");
        } while (!uint.TryParse(Console.ReadLine(), out arraySize));

        // read K
        uint elementsNumber;
        do
        {
            Console.Write("Enter number of the elements (K <= N): ");
        } while (
            !uint.TryParse(Console.ReadLine(), out elementsNumber) ||
            elementsNumber < 1 ||
            elementsNumber > arraySize
            );

        // read array[N] elements
        int[] intArray = new int[arraySize];
        for (int i = 0; i < intArray.Length; i++)
        {
            do
            {
                Console.Write("Enter array elements {0}: ", i + 1);
            } while (!int.TryParse(Console.ReadLine(), out intArray[i]));
        }

        // sort by descending order
        Array.Sort(intArray);
        Array.Reverse(intArray);

        // print first K element (the biggest)
        Console.Write("Maximal Sum is from numbers: (");
        int sum = 0;
        for (int i = 0; i < elementsNumber; i++)
        {
            int number = intArray[i];
            Console.Write(number);
            if (i != elementsNumber - 1)
            {
                Console.Write(" + ");
            }
            sum += number;
        }
        Console.WriteLine(") = {0}", sum);
    }
}
